﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string Depratment_Add(Depratment model, int ACID);


        [OperationContract]
        string Depratment_Update(Depratment model, int ACID);


        [OperationContract]
        string DepratmentByDepCode_Delete(string DepCode, int ACID);


        [OperationContract]
        Depratment DepratmentByDepCode_Get(string DepCode, int ACID);


        [OperationContract]
        List<Depratment> Depratment_Get(string sortBy, int ACID);

        [OperationContract]
        string depCode_max_get(string shopcode, int ACID);


    }
    public partial class Service : IService
    {
        #region  添加Depratment表数据
        /// <summary>
        /// 添加Depratment
        /// </summary>
        /// <param name="model">Depratment实体</param>
        /// <returns>空或错误</returns>
        public string Depratment_Add(Depratment model, int ACID)
        {
            if (string.IsNullOrEmpty(model.DepCode))
            {
                return "『部门编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.DepName))
            {
                return "『编码名称』不能为空！";
            }
            if (string.IsNullOrEmpty(model.shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Depratment.Count(p => p.DepCode == model.DepCode) > 0)
                    {
                        return "主键已存在！";
                    }
                    if (ctx.Depratment.Count(p => p.DepName == model.DepName) > 0)
                    {
                        return "编码名称已存在！";
                    }
                    ctx.Depratment.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加Depratment表数据事物用重构
        /// <summary>
        /// 添加Depratment事物用重构
        /// </summary>
        /// <param name="model">Depratment实体</param>
        /// <returns>空或错误</returns>
        private string Depratment_Add(Depratment model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.DepCode))
            {
                return "『部门编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.DepName))
            {
                return "『编码名称』不能为空！";
            }
            if (string.IsNullOrEmpty(model.shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            if (ctx.Depratment.Count(p => p.DepCode == model.DepCode) > 0)
            {
                return "主键已存在！";
            }
            if (ctx.Depratment.Count(p => p.DepName == model.DepName) > 0)
            {
                return "编码名称已存在！";
            }
            ctx.Depratment.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改Depratment表数据
        /// <summary>
        /// 修改Depratment
        /// </summary>
        /// <param name="model">Depratment实体</param>
        /// <returns>空或错误</returns>
        public string Depratment_Update(Depratment model, int ACID)
        {
            if (string.IsNullOrEmpty(model.DepCode))
            {
                return "『部门编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.DepName))
            {
                return "『编码名称』不能为空！";
            }
            if (string.IsNullOrEmpty(model.shopcode))
            {
                return "『分支机构编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Depratment.Count(p => p.DepCode == model.DepCode) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    Depratment Depratmentmodel = ctx.Depratment.Single(p => p.DepCode == model.DepCode);
                    if (!string.IsNullOrEmpty(model.DepCode))
                    {
                        Depratmentmodel.DepCode = model.DepCode;
                    }
                    if (!string.IsNullOrEmpty(model.shopcode))
                    {
                        Depratmentmodel.shopcode = model.shopcode;
                    }
                    if (!string.IsNullOrEmpty(model.DepName))
                    {
                        Depratmentmodel.DepName = model.DepName;
                    }
                    if (!string.IsNullOrEmpty(model.phone))
                    {
                        Depratmentmodel.phone = model.phone;
                    }
                    if (!string.IsNullOrEmpty(model.leader))
                    {
                        Depratmentmodel.leader = model.leader;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除Depratment表ByDepCode数据
        /// <summary>
        /// 删除DepratmentByDepCode
        /// </summary>
        /// <param name="DepCode">部门编号</param>
        /// <returns>空或错误</returns>
        public string DepratmentByDepCode_Delete(string DepCode, int ACID)
        {
            if (string.IsNullOrEmpty(DepCode))
            {
                return "『部门编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Depratment.Count(p => p.DepCode == DepCode) != 1)
                    {
                        return null;
                    }
                    Depratment model = ctx.Depratment.Single(p => p.DepCode == DepCode);
                    ctx.Depratment.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除Depratment表ByDepCode数据[事物用重构]
        /// <summary>
        /// 删除DepratmentByDepCode[事物用重构]
        /// </summary>
        /// <param name="DepCode">部门编号</param>
        /// <returns>空或错误</returns>
        private string DepratmentByDepCode_Delete(string DepCode, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(DepCode))
            {
                return "『部门编号』不能为空！";
            }
            if (ctx.Depratment.Count(p => p.DepCode == DepCode) != 1)
            {
                return null;
            }
            Depratment model = ctx.Depratment.Single(p => p.DepCode == DepCode);
            ctx.Depratment.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取Depratment表ByDepCode数据
        /// <summary>
        /// 获取DepratmentByDepCode
        /// </summary>
        /// <param name="DepCode">部门编号</param>
        /// <returns>当前表</returns>
        public Depratment DepratmentByDepCode_Get(string DepCode, int ACID)
        {
            if (string.IsNullOrEmpty(DepCode))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Depratment.Count(p => p.DepCode == DepCode) != 1)
                    {
                        return null;
                    }
                    return ctx.Depratment.SingleOrDefault(p => p.DepCode == DepCode);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取Depratment表数据
        /// <summary>
        /// 获取Depratment
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<Depratment></returns>
        public List<Depratment> Depratment_Get(Expression<Func<Depratment, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Depratment.ToList();
                        }
                        else
                        {
                            return ctx.Depratment.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Depratment.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.Depratment.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取Depratment表数据
        /// <summary>
        /// 获取Depratment
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<Depratment></returns>
        public List<Depratment> Depratment_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return Depratment_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取Depratment表数据计数
        /// <summary>
        /// 获取Depratment表数据计数
        /// </summary>
        /// <returns>List<Depratment></returns>
        public int Depratment_GetCount(Expression<Func<Depratment, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.Depratment.Count();

                    }
                    else
                    {
                        return ctx.Depratment.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取Depratment分页表数据
        /// <summary>
        /// 获取Depratment分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<Depratment></returns>
        public List<Depratment> Depratment_Getpage(int pageIndex, int pageSize, Expression<Func<Depratment, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Depratment.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.Depratment.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Depratment.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.Depratment.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改Depratment表数据
        /// <summary>
        /// 批量修改Depratment
        /// </summary>
        /// <param name="predicate">Expression<Func<Depratment, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<Depratment,Depratment>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int Depratment_batchUpdate(Expression<Func<Depratment, bool>> predicate, Expression<Func<Depratment, Depratment>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.Depratment.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改Depratment表数据事物用重构
        /// <summary>
        /// 批量修改Depratment事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<Depratment, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<Depratment,Depratment>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int Depratment_batchUpdate(Expression<Func<Depratment, bool>> predicate, Expression<Func<Depratment, Depratment>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.Depratment.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除Depratment表数据
        /// <summary>
        /// 批量删除Depratment
        /// </summary>
        /// <param name="predicate">Expression<Func<Depratment, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int Depratment_batchDel(Expression<Func<Depratment, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.Depratment.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除Depratment表数据事物用重构
        /// <summary>
        /// 批量删除Depratment事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<Depratment, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int Depratment_batchDel(Expression<Func<Depratment, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.Depratment.Delete(predicate);
            return ok;
        }
        #endregion

        public string depCode_max_get(string shopcode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (ctx.Depratment.Count(p=>p.shopcode==shopcode) > 0)
                {
                   return (int.Parse(ctx.Depratment.Where(p => p.shopcode == shopcode).Max(p => p.DepCode).Remove(0, 2)) + 1).ToString().PadLeft(2,'0');
                }
                else
                {
                    return shopcode + "01";
                }
            }
        }

    }
}

